//
//  RBRefreshHeader.m
//  FinancialStore
//
//  Created by kopuCoder on 16/9/1.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "RBRefreshHeader.h"

@interface RBRefreshHeader()

@property (weak, nonatomic) UILabel *label;

@property (nonatomic,weak)UIImageView *  bottomCircleImageView;
@property (nonatomic,weak)UIImageView *  logoImageView;
@property (nonatomic,weak)UIImageView *  successImageView;
@property (nonatomic,strong)CABasicAnimation * animation;
@property (nonatomic,assign)BOOL isNoMoreData;
@property (nonatomic,assign)BOOL isBeingRefresh;

@end

@implementation RBRefreshHeader
#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    // 设置控件的高度
    self.mj_h = 50;
    
    
    // 添加label
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor colorWithRed:0.02f green:0.41f blue:0.71f alpha:1.00f];
    label.font = [UIFont boldSystemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    
    
    // logo
    UIImageView *bottomCircleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_blue_circle"]];
    bottomCircleImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:bottomCircleImageView];
    self.bottomCircleImageView = bottomCircleImageView;
    
    // logo
  UIImageView *logoImageView = [UIImageView new];//[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_logo"]];
    logoImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:logoImageView];
    self.logoImageView = logoImageView;
    
    //successLogo
    UIImageView *successImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_blue_check"]];
    successImageView.hidden =YES;
    successImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:successImageView];
    self.successImageView = successImageView;
    
    __weak typeof(self)weakSelf = self;
    self.endRefreshingCompletionBlock = ^(){
        [weakSelf.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        GYNLog(@"刷新完成。。。");
    };
    
    [self updateConstraintsIfNeeded];
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
}

- (void)updateConstraints{
    [super updateConstraints];
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.mas_top).offset(15);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-15);
    }];
    
    [self.bottomCircleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.label.mas_left).offset(-15);
        make.height.with.mas_equalTo(20);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bottomCircleImageView.mas_centerX);
        make.centerY.mas_equalTo(self.bottomCircleImageView.mas_centerY);
        make.width.mas_equalTo(9);
        make.height.mas_equalTo(10);
    }];
    
    [self.successImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.bottomCircleImageView.mas_centerX);
        make.centerY.mas_equalTo(self.bottomCircleImageView.mas_centerY);
        make.width.mas_equalTo(11);
        make.height.mas_equalTo(10);
    }];
    
    
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    /**
     /** 普通闲置状态 */
    //    MJRefreshStateIdle = 1,
    /** 松开就可以进行刷新的状态 */
    //    MJRefreshStatePulling,
    /** 正在刷新中的状态 */
    //    MJRefreshStateRefreshing,
    /** 即将刷新的状态 */
    //    MJRefreshStateWillRefresh,
    /** 所有数据加载完毕，没有更多的数据了 */
    //    MJRefreshStateNoMoreData
    
    __weak typeof(self)weakSelf = self;
    switch (state) {
        case MJRefreshStateIdle:
            [self stopAnimation];
            self.label.text = @"下拉刷新";
            break;
        case MJRefreshStatePulling:
            self.label.text = @"释放加载";
            break;
        case MJRefreshStateWillRefresh:
            self.label.text = @"加载成功";
            break;
        case MJRefreshStateRefreshing:
            self.label.text = @"加载中...";
            [self startAnimation];
            break;
        default:
            break;
    }
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
//    if(pullingPercent>=1.0){
//        [self startAnimation];
//    }else{
//        [self stopAnimation];
//    }
    // 1.0 0.5 0.0
    // 0.5 0.0 0.5
//    self.label.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

- (void)changeLableText{
    self.label.text = @"下拉刷新";
    [UIView animateWithDuration:1 animations:^{
        
    }completion:^(BOOL finished) {
        self.label.text = @"释放加载";
    }];
}

- (CABasicAnimation *)animation{
    if (!_animation) {
        _animation = [CABasicAnimation animation];
        _animation.keyPath = @"transform.rotation.z";
        _animation.fromValue = @(0);
        _animation.toValue = @(M_PI*2);
        _animation.duration  = 0.5;
        _animation.repeatCount = MAXFLOAT;
    }
    return _animation;
}

- (void)startAnimation{
    if(self.isBeingRefresh) return;
    [self.bottomCircleImageView.layer addAnimation:self.animation forKey:@"logoKey"];
    self.isBeingRefresh = YES;
}

- (void)stopAnimation{
    self.isBeingRefresh = NO;
    [self.bottomCircleImageView.layer removeAnimationForKey:@"logoKey"];
}

@end
